package com.jjh.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 一个项目只能有一个线程池
 * @author MyJjh
 *
 */
public class ExecutorsUtil
{
	/**
	 * 线程池 
	 */
	private static ExecutorService executorService = Executors.newCachedThreadPool();
	
	/**
	 * 提交一个线程,但是不等待
	 * @param task
	 */
	public static void submit(Callable<Object> task)
	{
		executorService.submit(task);
	}
	
	/**
	 * 等待提交线程
	 * @param taskList
	 */
	public static void invokeAll(List<Callable<Object>> taskList)
	{
		try
		{
			executorService.invokeAll(taskList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭线程
	 */
	public static void shutdown()
	{
		executorService.shutdown();
	}
	
	
	public static void main(String [] args)
	{
		ExecutorsUtil executorsUtil = new ExecutorsUtil();
		
		List<Callable<Object>> taskList = new ArrayList<Callable<Object>>();
		
		taskList.add( () ->
		{
			for(int i = 0 ; i < 10 ; i++ )
			{
				System.out.println(Thread.currentThread() + "===" + i);
			}
			return null ;
		});
		
		executorsUtil.invokeAll(taskList);
		
		System.out.println(Thread.currentThread() + "==main===");
	}

}
